// TableMagic - some useful methods for dealing with tableViews tied to controllers.

#import "TableMagic.h"

@implementation NXTableView (tableMagic)
+ keyTableView
{
    id view = [[NXApp mainWindow] firstResponder];
    return ([view isKindOf:[NXTableView class]]) ? view : nil;
}

- (EOController *)eoController
{
    id assoc = [self delegate];
    if ([assoc isKindOfClass:[EOAssociation class]])
        return [assoc controller];
    else
        return nil;
}

- (NSString *)associationKeyForColumn: (unsigned int)colNum
{
    id vector = [self columnAt:colNum];
    id assoc  = [vector identifier];
    return ([assoc isKindOfClass:[EOAssociation class]]) ? [assoc key] : nil;
}

- (NSArray *)associationKeysForSelectedColumns
{
    int numselected = [self selectedColumnCount];
    int colNum;
    NSMutableArray *keys;

    keys = [NSMutableArray arrayWithCapacity:numselected];

    colNum = NX_NoIndex;
    while ((colNum = [self selectedColumnAfter:colNum])!=NX_NoIndex) {
        [keys addObject:[self associationKeyForColumn:colNum]];
    }

    return keys;
}
@end
